// --- JQueryBizObjListAnimi.V2.js ---
function JquerySlideShow(pname) {
    this.parentEl = $('#' + pname);
    this.timer = null;
    this.slideAnimating = false;
    this.mouseInSlide = false;
    this.tickOnMouseLeave = false;
    this.slideTimers = new Array();
    this.cmdQ = new Array();
    eval(pname + "a=this");
    this.resetSlideShowState();

    var width = this.getSlideInfo(null, "ssWidth", "0");
    var height = this.getSlideInfo(null, "ssHeight", "0");
    if (width != 0 && height != 0) {
        this.parentEl.find(".ksg-slideshow").width(width).height(height);
        this.parentEl.width(width).height(height);
    }

    var me = this;
    this.parentEl.mouseenter(function () {
        me.mouseEnter();
    });

    this.parentEl.mouseleave(function () {
        me.mouseLeave();
    });

    this.setupInitialSlideEffects();

    if (this.getConfigBool(null, "autoStart", true)) {
        var startDelay = parseInt(this.getSlideInfo(null, "startDelay", "0"));
        this.timer = setTimeout(function () {
            var chlds = me.parentEl.find(".ksg-slide");
            me.callSlideFunction($(chlds[me.pos]), true);
            me.animateSlideLayers($(chlds[me.pos]), me);
            me.timer = setTimeout(pname + "a.tick()", me.getSlideTimeout($(chlds[me.pos])));
        }, startDelay);
    }
}

JquerySlideShow.prototype.SlideAnimators = new Object();

JquerySlideShow.prototype.setupInitialSlideEffects = function () {
    var me = this;
    this.parentEl.find(".ksg-slide-layer").each(function (index) {
        var uo = me.getConfigBool($(this), "useOutline", false);
        var us = me.getConfigBool($(this), "useShadow", false);
        var um = me.getConfigBool($(this), "useMirror", false);
        var options = {
            outline: uo, outlineColor1: "#808080", outlineColor2: "#808080", outlineWeight: 1,
            shadow: us, shadowColor: "#000", shadowBlur: 1, shadowOffsetTop: 3, shadowOffsetLeft: 3, shadowOpacity: 0.08,
            mirror: um, mirrorColor: "#000"
        };
        if (uo || us)
            $(this).find("h1").FontEffect(options);
    });
}


JquerySlideShow.prototype.mouseEnter = function () {
    this.mouseInSlide = true;
}

JquerySlideShow.prototype.mouseLeave = function () {
    this.mouseInSlide = false;
    if (this.tickOnMouseLeave) {
        this.tick();
        this.tickOnMouseLeave = false;
    }
}

JquerySlideShow.prototype.go = function () {
    if (this.timer)
        return;
    this.tick();
}

JquerySlideShow.prototype.tick = function (dontDoNextTick, doPrev) {
    this.slideAnimating = true;
    var lp = this.pos;
    var chlds = this.parentEl.find(".ksg-slide");
    if (chlds.length <= 1) {
        chlds.css("display", "block");
        return;
    }

    if (this.mouseInSlide && this.getConfigBool(null, "pauseWhileMouseInSlide", false)) {
        this.tickOnMouseLeave = true;
        return;
    }

    var curSlide = { pos: this.pos, obj: this.pos != -1 ? chlds.eq(lp) : null };
    var nextSlide = this.getNextVisibleSlide(lp, doPrev);
    var aniSpeed = this.getSlideAnimationSpeed(curSlide.obj, nextSlide.obj);

    if (curSlide.obj) {
        this.callSlideFunction(curSlide.obj, false);
        this.hideSlideLayers(curSlide.obj);
    }

    this.getAnimiFunction(nextSlide.obj)(this, aniSpeed, curSlide, nextSlide, doPrev);
    this.callSlideFunction(nextSlide.obj, true);
    this.animateSlideLayers(nextSlide.obj, this);

    this.pos = nextSlide.pos;
    if (dontDoNextTick != true && !(nextSlide.onLast && this.getConfigBool(null, "loop", true) == false))
        this.timer = setTimeout(this.parentEl.attr("id") + "a.tick()", this.getSlideTimeout(nextSlide.obj));
}

JquerySlideShow.prototype.hideSlideLayers = function (slide) {
    for (var i in this.slideTimers)
        clearTimeout(this.slideTimers[i]);
    this.slideTimers = new Array();
    slide.find(".ksg-slide-layer").stop().hide();
}

JquerySlideShow.prototype.animateSlideLayers = function (slide, slideShow) {
    slide.find(".ksg-slide-layer").each(function (index, layer) {

        var layerDelay = $(layer).attr("startDelay");
        layertDelay = layerDelay == null ? 0 : parseInt(layerDelay);
        var animiTime = $(layer).attr("animiTime");
        animiTime = (animiTime == null || animiTime == "") ? 5000 : parseInt(animiTime);
        var startCss = JquerySlideShow.prototype.getCssObject($(layer).attr("startCss"));
        var endCss = JquerySlideShow.prototype.getCssObject($(layer).attr("endCss"));
        slideShow.slideTimers.push(setTimeout(function () {
            $(layer).css(startCss).show();
            if (endCss.length != 0)
                $(layer).animate(endCss, animiTime,
                function () {
                    // Animation complete.
                });
        }, layerDelay));

    });
}

JquerySlideShow.prototype.getCssObject = function (cssString) {
    return (cssString != null && cssString.length != 0) ? eval("(" + cssString + ")") : new Object();
}

JquerySlideShow.prototype.getAnimiFunction = function (slide) {
    return JquerySlideShow.prototype.SlideAnimators[this.getSlideInfo(slide, "transition", "fade")];
}

JquerySlideShow.prototype.slideAnimationComplete = function () {
    this.slideAnimating = false;
    if (this.cmdQ.length != 0)
        this.move(this.cmdQ.pop());
}

JquerySlideShow.prototype.stop = function () {
    this.pause();
    this.resetSlideShowState();
}

JquerySlideShow.prototype.pause = function () {
    if (this.timer) {
        window.clearTimeout(this.timer);
        this.timer = null;
    }
}

JquerySlideShow.prototype.move = function (doPrev) {
    if (this.slideAnimating) {
        this.cmdQ.unshift(doPrev);
        return;
    }
    var isOff = this.timer == null;
    this.pause();
    this.tick(isOff, doPrev);
}

JquerySlideShow.prototype.resetClearMemoryGo = function () {
    this.clearShownOnFirstMemory();
    this.stop();

    var chlds = this.parentEl.find(".ksg-slide");
    this.callSlideFunction($(chlds[this.pos]), true);
    this.animateSlideLayers($(chlds[this.pos]), me);
    this.timer = setTimeout(pname + "a.tick(false)", this.getSlideTimeout($(chlds[this.pos])));
}


JquerySlideShow.prototype.getNextVisibleSlide = function (lp, doPrev) {
    var chlds = this.parentEl.find(".ksg-slide");
    var p = arguments.length == 0 ? -1 : lp;
    var c = null;
    do {
        if (doPrev)
            p = (p <= 0) ? (chlds.length - 1) : (p - 1);
        else
            p = (p >= (chlds.length - 1)) ? 1 : (p + 1);
        c = chlds.eq(p);
    } while (!this.shouldShowSlide(c));
    return { pos: p, obj: c, onLast: p == chlds.length - 1 };
}

JquerySlideShow.prototype.resetSlideShowState = function () {
    this.pos = -1;
    var lp = this.pos, prt = this.parentEl, chlds = prt.find(".ksg-slide");
    chlds.css("top", "0px").css("display", "none");

    if (chlds.length != 0) {
        var si = this.getNextVisibleSlide();
        this.pos = si.pos;
        si.obj.show();
        return;
    }
}



JquerySlideShow.prototype.clearShownOnFirstMemory = function () {
    $.cookie(this.parentEl.attr("id"), "");
}

JquerySlideShow.prototype.shouldShowSlide = function (slide) {
    var s = !slide.hasClass("FirstArticle") && !slide.hasClass("FirstChild");
    if (s) {
        if (this.getConfigBool(slide, "onlyShowOnFirstViewing", false)) {
            var c = $.cookie(this.parentEl.attr("id"));
            if (c != "t")
                $.cookie(this.parentEl.attr("id"), "t");
            else
                s = false;
        }
    }

    return s;
}

JquerySlideShow.prototype.getConfigBool = function (slide, name, def) {
    return this.getSlideInfo(slide, name, def == true ? "true" : "false").toLowerCase() == "true";
}

JquerySlideShow.prototype.getSlideTimeout = function (slide) {
    //timeouts are specified in 10ths of a second, but javascript timeout uses millisceonds
    return this.getSlideInfo(slide, "showTime", 40) * 100;
}

JquerySlideShow.prototype.callSlideFunction = function (slide, isShowing) {
    var slideId = this.getSlideInfo(slide, "slideId", null);
    if (slideId != null) {
        try {
            var f = eval((isShowing ? "show_" : "hide_") + slideId);
            if (typeof f == "function")
                f(slide, this);
        }
        catch (e) {
        }
    }
}

JquerySlideShow.prototype.getSlideInfo = function (slide, name, defVal) {
    var t = null, slideInfo = null;
    if (slide != null)
        t = slide.attr(name);
    if (t == null && slide != null)
        t = slide.find(".ksg-slide-info").attr(name);

    if (t == null)
        t = this.parentEl.find(".ksg-slideshow-info").attr(name);

    if (t == null || t == "")
        t = defVal;
    return t;
}

JquerySlideShow.prototype.getSlideAnimationSpeed = function (item, infoItem) {
    if (infoItem == null)
        infoItem = item;
    var sp = this.getSlideInfo(infoItem, "animiSpeed", "normal");
    if (sp != "normal" && sp != "slow" && sp != "fast")
        sp = parseInt(sp);

    return sp;
}

JquerySlideShow.prototype.SlideAnimators.fade = function (slideShow, aniSpeed, slideOut, slideIn) {

    slideIn.obj.css("top", "0px").css("left", "0px").css("display", "none");
    if (slideOut.obj)
        slideOut.obj.fadeOut(aniSpeed);
    slideIn.obj.fadeIn(aniSpeed, function () { slideShow.slideAnimationComplete(); });
}

JquerySlideShow.prototype.SlideAnimators.rotate = function (slideShow, aniSpeed, slideOut, slideIn, doPrev) {
    var w = slideShow.parentEl.width();
    var dir = doPrev ? '+=' : '-=';
    var leftPos = doPrev ? (-w) : w;
    var easing = slideShow.cmdQ.length != 0 ? 'linear' : 'swing';

    slideIn.obj.css("top", "0px").css("display", "block").css("left", leftPos);
    slideIn.obj.animate({ left: dir + w }, 800, easing, function () { slideShow.slideAnimationComplete(); });
    if (slideOut)
        slideOut.obj.animate({ left: dir + w }, 800, easing);
}
